from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from decouple import config
from pydantic import EmailStr


conf = ConnectionConfig(
    MAIL_USERNAME=config('MAIL_USERNAME'),   # 在当前目录下新建一个.env文件，把环境变量和其对应的值写入，config自动读取
    MAIL_PASSWORD=config('MAIL_PASSWORD'),
    MAIL_FROM=config('MAIL_FROM'),
    MAIL_PORT=config('MAIL_PORT'),
    MAIL_SERVER=config('MAIL_SERVER'),
    MAIL_TLS=True,
    MAIL_SSL=False,
    USE_CREDENTIALS=True
)


async def send_mail(email: EmailStr, body: str) -> None:
    message = MessageSchema(
        subject="FastAPI JWT Auth Demo",
        recipients=[email],
        body=body,
        subtype="html"
    )

    fm = FastMail(conf)
    await fm.send_message(message)
